package com.richard.myapplication.smallViewpager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;
import android.widget.Toast;

import com.richard.myapplication.R;
import com.richard.myapplication.util.DisplayUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SmallViewPagerActivity extends AppCompatActivity {
    private SwipeDirectionViewPager vp_content;
    private List<Integer> colorList = Arrays.asList(R.color.colorPrimary, R.color.colorPrimaryDark, R.color.colorPrimary, R.color.colorAccent, R.color.color_red, R.color.bg_lunch_selected, R.color.bg_supper_selected);
    private List<Integer> colorsRes = new ArrayList<>(colorList);
    private List<String> texts = Arrays.asList("第⑦个","第一个","第二个","第三个","第四个","第五个","第六个","第⑦个","第一个");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_small_view_pager);
        vp_content = findViewById(R.id.vp_content);

        //首位相连
        int head = colorsRes.get(0);
        int tail = colorsRes.get(colorsRes.size() - 1);
        colorsRes.add(0, tail);
        colorsRes.add(head);

        final CommonPagerAdapter<Integer> horizontalStackAdapter = new CommonPagerAdapter<Integer>(colorsRes) {
            @Override
            public void renderItemView(@NonNull View itemView, final int position) {
                ((TextView) itemView).setText(texts.get(position));
                int color = getResources().getColor(getBindItemData(position));
                GradientDrawable drawable = (GradientDrawable) itemView.getBackground();
                if (drawable == null) {
                    drawable = new GradientDrawable();
                    itemView.setBackground(drawable);
                }
                drawable.setCornerRadius(DisplayUtil.dp2px(getBaseContext(), 5));
                drawable.setStroke(DisplayUtil.dp2px(getBaseContext(), 5), getResources().getColor(R.color.color_light_grey));
                drawable.setColor(color);
            }

            @NonNull
            @Override
            public View getPageItemView(@NonNull ViewGroup container, int position) {
                View view = LayoutInflater.from(SmallViewPagerActivity.this).inflate(R.layout.item_vp_test, container, false);
                view.setTag(String.valueOf(position));
                return view;
            }
        };
        horizontalStackAdapter.setOnItemClickListener(new CommonPagerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getApplicationContext(), "当前点击第" + texts.get(position), Toast.LENGTH_SHORT).show();
            }
        });




        vp_content.setOffscreenPageLimit(3);
        vp_content.setPageTransformer(false, new TransWithRotationTransformer(vp_content));
        vp_content.setAdapter(horizontalStackAdapter);

        vp_content.addOnPageChangeListener(new CycleScrollOnPageChangeListener(vp_content, colorsRes.size()));
        vp_content.setCurrentItem(1, false);
        vp_content.setAllowedSwipeDirection(SwipeDirectionViewPager.SwipeDirection.left);

        findViewById(R.id.btn_start_shake).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startShakeAnim(vp_content.findViewWithTag(vp_content.getCurrentItem()));
            }
        });
    }

    private void startShakeAnim(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 0, -DisplayUtil.dp2px(this, 20));
        animator.setDuration(500);
        animator.setRepeatCount(5);
        animator.setStartDelay(100);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.start();
    }

}
